package com.example.myapplication;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link BlankFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class BlankFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    int n = 0;
    public BlankFragment() {
        // Required empty public constructor
    }
    public static BlankFragment newInstance(String param1, String param2) {
        BlankFragment fragment = new BlankFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // 加载布局文件，生成对应的view
        View view = inflater.inflate(R.layout.fragment_blank, container, false);
        Button button = view.findViewById(R.id.left_btn);  // 在view上面通过id查找组件，相当于在布局文件中查找组件
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view1) {
                // 回调函数
                n--;
                TextView textView = view.findViewById(R.id.text);
                textView.setText(n + "");
            }
        });

        Button button2 = view.findViewById(R.id.right_btn);  // 在view上面通过id查找组件，相当于在布局文件中查找组件
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view1) {
                // 回调函数
                n++;
                TextView textView = view.findViewById(R.id.text);
                textView.setText(n + "");
            }
        });

        return view;    // 返回view，相当于返回布局文件的内容
    }
}